<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Handling Generics  Reference</title>
    <link rel="stylesheet" type="text/css" href="css/jazzy.css" />
    <link rel="stylesheet" type="text/css" href="css/highlight.css" />
    <meta charset='utf-8'>
    <script src="js/jquery.min.js" defer></script>
    <script src="js/jazzy.js" defer></script>
    
    <script src="js/lunr.min.js" defer></script>
    <script src="js/typeahead.jquery.js" defer></script>
    <script src="js/jazzy.search.js" defer></script>
  </head>
  <body>
    <a title="Handling Generics  Reference"></a>
    <header>
      <div class="content-wrapper">
        <p><a href="index.html">SwiftyMocky 4.2.0 Docs</a> (86% documented)</p>
        <p class="header-right"><a href="https://github.com/MakeAWishFoundation/SwiftyMocky"><img src="img/gh.png" alt="GitHub"/>View on GitHub</a></p>
        <div class="header-right">
          <form role="search" action="search.json">
            <input type="text" placeholder="Search documentation" data-typeahead>
          </form>
        </div>
      </div>
    </header>
    <div class="content-wrapper">
      <p id="breadcrumbs">
        <a href="index.html">SwiftyMocky Reference</a>
        <img id="carat" src="img/carat.png" alt=""/>
        Handling Generics  Reference
      </p>
    </div>
    <div class="content-wrapper">
      <nav class="sidebar">
        <ul class="nav-groups">
          <li class="nav-group-name">
            <a href="Main%20Guides.html">Main Guides</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="overview.html">Overview</a>
              </li>
              <li class="nav-group-task">
                <a href="installation.html">Installation</a>
              </li>
              <li class="nav-group-task">
                <a href="setup-in-project.html">Setup in project</a>
              </li>
              <li class="nav-group-task">
                <a href="mockfile.html">Mockfile</a>
              </li>
              <li class="nav-group-task">
                <a href="command-line-interface.html">Command Line Interface</a>
              </li>
              <li class="nav-group-task">
                <a href="supported-features.html">Supported features</a>
              </li>
              <li class="nav-group-task">
                <a href="prototyping.html">Prototyping</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Additional%20Guides.html">Additional Guides</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="matcher-support-for-not-equatable.html">Matcher support for not Equatable</a>
              </li>
              <li class="nav-group-task">
                <a href="handling-generics.html">Handling Generics</a>
              </li>
              <li class="nav-group-task">
                <a href="examples.html">Examples</a>
              </li>
              <li class="nav-group-task">
                <a href="legacy.html">Legacy</a>
              </li>
              <li class="nav-group-task">
                <a href="add-xcode-generate-action.html">Add XCode generate action</a>
              </li>
              <li class="nav-group-task">
                <a href="known-issues.html">Known issues</a>
              </li>
              <li class="nav-group-task">
                <a href="changelog.html">CHANGELOG</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Global%20methods.html">Global methods</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Global%20methods.html#/s:11SwiftyMocky5Givenyyx_ABQzAA14StubbingPolicyOtAA4MockRzlF">Given(_:_:_:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Global%20methods.html#/s:11SwiftyMocky5Givenyyxm_06StaticC0QzAA14StubbingPolicyOtAA0D4MockRzlF">Given(_:_:_:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Global%20methods.html#/s:11SwiftyMocky7Performyyx_ABQztAA4MockRzlF">Perform(_:_:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Global%20methods.html#/s:11SwiftyMocky7Performyyxm_06StaticC0QztAA0D4MockRzlF">Perform(_:_:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Global%20methods.html#/s:11SwiftyMocky6Verify__4file4lineyx_ABQzs12StaticStringVSutAA4MockRzlF">Verify(_:_:file:line:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Global%20methods.html#/s:11SwiftyMocky6Verify__4file4lineyxm_06StaticC0Qzs0F6StringVSutAA0F4MockRzlF">Verify(_:_:file:line:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Global%20methods.html#/s:11SwiftyMocky6Verify___4file4lineyx_AA5CountOABQzs12StaticStringVSutAA4MockRzlF">Verify(_:_:_:file:line:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Global%20methods.html#/s:11SwiftyMocky6Verify___4file4lineyxm_AA5CountO06StaticC0Qzs0G6StringVSutAA0G4MockRzlF">Verify(_:_:_:file:line:)</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Configuration.html">Configuration</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Enums/StubbingPolicy.html">StubbingPolicy</a>
              </li>
              <li class="nav-group-task">
                <a href="Enums/SequencingPolicy.html">SequencingPolicy</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Types.html">Types</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Protocols/Mock.html">Mock</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/StaticMock.html">StaticMock</a>
              </li>
              <li class="nav-group-task">
                <a href="Enums/Parameter.html">Parameter</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Matcher.html">Matcher</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Matcher/ComparisonResult.html">– ComparisonResult</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/Matcher/ParameterComparisonResult.html">– ParameterComparisonResult</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/Countable.html">Countable</a>
              </li>
              <li class="nav-group-task">
                <a href="Enums/Count.html">Count</a>
              </li>
              <li class="nav-group-task">
                <a href="Structs/Stubber.html">Stubber</a>
              </li>
              <li class="nav-group-task">
                <a href="Structs/StubberThrows.html">StubberThrows</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Generics.html">Generics</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Structs/GenericAttribute.html">GenericAttribute</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Helpers.html">Helpers</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Helpers.html#/s:11SwiftyMocky20XCTAssertThrowsError_2of_4file4lineyxyKXK_q_mSSyXKs12StaticStringVSuts0E0R_r0_lF">XCTAssertThrowsError(_:of:_:file:line:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Helpers.html#/s:11SwiftyMocky20XCTAssertThrowsError_5error_4file4lineyxyKXK_q_SSyXKs12StaticStringVSutSQR_s0E0R_r0_lF">XCTAssertThrowsError(_:error:_:file:line:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/MockyAssertion.html">MockyAssertion</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Internal.html">Internal</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Classes/SwiftyMockyTestObserver.html">SwiftyMockyTestObserver</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/SwiftyMockyTestObserver.html">SwiftyMockyTestObserver</a>
              </li>
              <li class="nav-group-task">
                <a href="Structs/FatalErrorUtil.html">FatalErrorUtil</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/OptionalType.html">OptionalType</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/WithSequencingPolicy.html">WithSequencingPolicy</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/WithStaticSequencingPolicy.html">WithStaticSequencingPolicy</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/WithStubbingPolicy.html">WithStubbingPolicy</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/Int.html">Int</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/Optional.html">Optional</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/UInt.html">UInt</a>
              </li>
              <li class="nav-group-task">
                <a href="Enums/MockError.html">MockError</a>
              </li>
              <li class="nav-group-task">
                <a href="Enums/StubProduct.html">StubProduct</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/StubbedMethod.html">StubbedMethod</a>
              </li>
              <li class="nav-group-task">
                <a href="Internal.html#/s:11SwiftyMocky7Failureys5NeverOSSF">Failure(_:)</a>
              </li>
              <li class="nav-group-task">
                <a href="Internal.html#/s:11SwiftyMocky0B6Assert__4file4lineySbyXK_SSyXKs12StaticStringVSutF">MockyAssert(_:_:file:line:)</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Guides.html">Other Guides</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="contents.html">Contents</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Classes.html">Other Classes</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Classes/ArgumentCaptor.html">ArgumentCaptor</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Enums.html">Other Enumerations</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Enums/MockScope.html">MockScope</a>
              </li>
              <li class="nav-group-task">
                <a href="Enums/Utils.html">Utils</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Protocols.html">Other Protocols</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Protocols/TypeErasedValue.html">TypeErasedValue</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Other%20Structs.html">Other Structures</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Structs/TypeErasedAttribute.html">TypeErasedAttribute</a>
              </li>
            </ul>
          </li>
        </ul>
      </nav>
      <article class="main-content">
        <section>
          <section class="section">
            
            <h1 id='generics-support' class='heading'>Generics Support</h1>

<p>In <strong>SwiftyMocky</strong>, we are supporting generics for methods generation, as well as for generic constrained methods in mocked protocols.</p>
<h2 id='protocols-with-generic-methods' class='heading'>Protocols with generic methods</h2>

<p>We support generic constrained methods in protocols definition, so something like below:</p>
<pre class="highlight swift"><code><span class="c1">//sourcery: AutoMockable</span>
<span class="kd">public</span> <span class="kd">protocol</span> <span class="kt">HistorySectionMapperType</span><span class="p">:</span> <span class="kd">class</span> <span class="p">{</span>
    <span class="kd">func</span> <span class="n">map</span><span class="o">&lt;</span><span class="kt">T</span><span class="p">:</span> <span class="kt">DateSortable</span><span class="o">&gt;</span><span class="p">(</span><span class="n">_</span> <span class="nv">items</span><span class="p">:</span> <span class="p">[</span><span class="kt">T</span><span class="p">])</span> <span class="o">-&gt;</span>  <span class="p">[(</span><span class="nv">key</span><span class="p">:</span> <span class="kt">String</span><span class="p">,</span> <span class="nv">items</span><span class="p">:</span> <span class="p">[</span><span class="kt">T</span><span class="p">])]</span>
<span class="p">}</span>
</code></pre>

<p>Will generate valid mock.</p>

<p>Please have in mind, that generic methods are problematic in a lot of cases, so you could experience:</p>

<ol>
<li>AutoComplete issues for given, perform and verify: - use full <code>&lt;MockName&gt;.Given.</code> for getting autocomplete.</li>
<li>Use full <code>.any(Value.Type)</code> instead of <code>.any</code>, to avoid ambiguity.</li>
<li>Matching issues - if you are using generic parameters as <code>.value</code>, you might need to add additional comparators to <code><a href="Classes/Matcher.html">Matcher</a></code> instance. However, it should be handled for most of basic types.</li>
<li>When working with methods like <code>func decode&lt;T&gt;(_ type: T.Type, data: Data) -&gt; T</code>, it will require to register comparator for every custom <code>T.Type</code> used, like <code>Matcher.default.register(CustomType.Type.self)</code> - or just use <code>.any(Custom.self)</code></li>
</ol>
<h2 id='protocols-with-generic-subscripts' class='heading'>Protocols with generic subscripts</h2>

<p>We support generic subscript, but for now they need to annotated manually, like below:</p>
<pre class="highlight swift"><code><span class="c1">//sourcery: AutoMockable</span>
<span class="kd">public</span> <span class="kd">protocol</span> <span class="kt">ProtocolWithGenericSubscript</span><span class="p">:</span> <span class="kd">class</span> <span class="p">{</span>
    <span class="c1">//sourcery: associatedtype = "T: Sequence"</span>
    <span class="kd">subscript</span><span class="o">&lt;</span><span class="kt">T</span><span class="p">:</span> <span class="kt">Sequence</span><span class="o">&gt;</span><span class="p">(</span><span class="n">_</span> <span class="nv">items</span><span class="p">:</span> <span class="p">[</span><span class="kt">T</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="kt">Int</span> <span class="k">where</span> <span class="kt">T</span><span class="o">.</span><span class="kt">Element</span><span class="p">:</span> <span class="kt">Equatable</span> <span class="p">{</span> <span class="k">get</span> <span class="k">set</span> <span class="p">}</span>
<span class="p">}</span>
</code></pre>

<p>Annotations associatedtype should contain everything in &lt;&gt;, while where at the end gets autoparsed. That will generate a valid mock.</p>

<p>Please have in mind, that generic methods are problematic in a lot of cases, so you could experience. They generally match the ones of generic method above.</p>
<h2 id='protocols-with-associated-types' class='heading'>Protocols with associated types</h2>

<p>We are also supporting protocols with associated types (mock will be generated as generic class), but that requires some aditional manuall annotations:</p>
<pre class="highlight swift"><code><span class="c1">//sourcery: AutoMockable</span>
<span class="c1">//sourcery: associatedtype = "T1: Sequence"</span>
<span class="c1">//sourcery: associatedtype = "T2: Comparable, EmptyProtocol"</span>
<span class="kd">protocol</span> <span class="kt">AVeryAssociatedProtocol</span> <span class="p">{</span>
    <span class="kd">associatedtype</span> <span class="kt">T1</span><span class="p">:</span> <span class="kt">Sequence</span>
    <span class="kd">associatedtype</span> <span class="kt">T2</span><span class="p">:</span> <span class="kt">Comparable</span><span class="p">,</span> <span class="kt">EmptyProtocol</span>

    <span class="kd">func</span> <span class="nf">fetch</span><span class="p">(</span><span class="k">for</span> <span class="nv">value</span><span class="p">:</span> <span class="kt">T2</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">T1</span>
<span class="p">}</span>
</code></pre>

<p>Definition above will produce:</p>
<pre class="highlight swift"><code><span class="kd">class</span> <span class="kt">AVeryAssociatedProtocolMock</span><span class="o">&lt;</span><span class="kt">TypeT1</span><span class="p">,</span><span class="kt">TypeT2</span><span class="o">&gt;</span><span class="p">:</span> <span class="kt">AVeryAssociatedProtocol</span><span class="p">,</span> <span class="kt">Mock</span> <span class="k">where</span> <span class="kt">TypeT1</span><span class="p">:</span> <span class="kt">Sequence</span><span class="p">,</span> <span class="kt">TypeT2</span><span class="p">:</span> <span class="kt">Comparable</span><span class="p">,</span> <span class="kt">TypeT2</span><span class="p">:</span> <span class="kt">EmptyProtocol</span> <span class="p">{</span>

    <span class="kd">typealias</span> <span class="kt">T1</span> <span class="o">=</span> <span class="kt">TypeT1</span>
    <span class="kd">typealias</span> <span class="kt">T2</span> <span class="o">=</span> <span class="kt">TypeT2</span>

<span class="c1">// ...</span>
</code></pre>
<h2 id='generic-protocols-constrained-to-type' class='heading'>Generic Protocols constrained to type</h2>

<p>In some scenarios, you might end up defining protocol &ldquo;inheriting&rdquo; from generic protocol and constraining it to some specific type. You can use <strong>typealias</strong> annotations to resolve this scenario. Consider following example:</p>
<pre class="highlight swift"><code><span class="kd">protocol</span> <span class="kt">SomeObjectRepository</span><span class="p">:</span> <span class="kt">Repository</span> <span class="k">where</span> <span class="kt">Entity</span> <span class="o">==</span> <span class="kt">SomeObject</span> <span class="p">{</span> <span class="p">}</span>

<span class="kd">protocol</span> <span class="kt">Repository</span> <span class="p">{</span>
    <span class="n">associatedType</span> <span class="kt">Entity</span><span class="p">:</span> <span class="kt">EntityType</span>

    <span class="kd">func</span> <span class="nf">store</span><span class="p">(</span><span class="n">_</span> <span class="nv">entity</span><span class="p">:</span> <span class="kt">Entity</span><span class="p">)</span>
    <span class="kd">func</span> <span class="nf">getEntity</span><span class="p">(</span><span class="nv">id</span><span class="p">:</span> <span class="kt">String</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Entity</span>
<span class="p">}</span>

<span class="kd">protocol</span> <span class="kt">EntityType</span> <span class="p">{</span>
    <span class="k">var</span> <span class="nv">id</span><span class="p">:</span> <span class="kt">String</span> <span class="p">{</span> <span class="k">get</span> <span class="p">}</span>
<span class="p">}</span>
</code></pre>

<p>In order to generate valid mock, you need to define what <code>Entity</code> is for <code>SomeObjectRepository</code>. You can do it by &ldquo;annotating&rdquo; typealias:</p>
<pre class="highlight swift"><code><span class="c1">//sourcery: AutoMockable</span>
<span class="c1">//sourcery: typealias = "Entity = SomeObject"</span>
<span class="kd">protocol</span> <span class="kt">SomeObjectRepository</span><span class="p">:</span> <span class="kt">Repository</span> <span class="k">where</span> <span class="kt">Entity</span> <span class="o">==</span> <span class="kt">SomeObject</span> <span class="p">{</span> <span class="p">}</span>
</code></pre>

<p>That would result in generating valid mock.</p>

          </section>
        </section>
        <section id="footer">
          <p>Copyright © 2017 MakeAWishFoundation. All rights reserved.</p>
          <p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.14.2</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
        </section>
      </article>
    </div>
  </body>
</html>
